<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><title>DDL - Create Trigger</title>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="expires" content="" />
<meta name="lang" content="en-US" />
<meta name="template-info" content="topic.htm 2012-03-19 usp" />
<meta name="creation-date" scheme="iso8601" content="2013-06-19" />
<meta name="last-modified" scheme="iso8601" content="2013-06-19" />
<meta name="publish-date" scheme="iso8601" content="" />
<meta name="version" content="1" />
<meta name="author" content="USP" />
<meta name="robots" content="follow" />
<meta name="category" content="Programming/Database/Firebird" />
<meta name="keywords" content="programming, database, Firebird, SQL, documentation." />
<meta name="description" content="" />

<link rel="stylesheet" type="text/css" href="../../../includes/topic-2.css" />
<link rel="stylesheet" type="text/css" href="../local.css" />

<script language="JavaScript" type="text/javascript" src="../../../includes/global.js"></script>
<script language="JavaScript" type="text/javascript" src="../../../includes/cookies.js"></script>
<script language="JavaScript" type="text/javascript" src="../../../includes/topic.js"></script>

</head><body class="content" onload="onloadTopic()"><a name="top"></a>

<h1>Create Trigger</h1>

<pre class="syntax">create trigger <i>TriggerName</i> for <i>TableName</i>
    [ active | inactive ]
    { before | after } <i>trigger-actions</i>
    [ position <i>number</i> ]
    as
    begin
    <i>trigger-instructions</i>
    end
</pre>

<pre class="syntax">
trigger-actions := <i>trigger-action</i> [ or <i>trigger-action</i> ]...
trigger-action := { insert | update | delete }
</pre>


<h2>Example</h2>

<pre class="code">
create trigger myTrigger for myTable
  before insert or update
as
begin
  if ( inserting and new.id is null ) then new.id = gen_id ( myTableGenerator, 1 );
  myTable.DateCreated = current_timestamp;
end
</pre>

<pre class="code">
create trigger myTrigger for myTable
  before insert or update
as
begin
  if ( inserting and new.id is null ) then new.id = gen_id ( myTableGenerator, 1 );
  else if ( updating ) then new.ts = old.ts + 1;
  myTable.DateCreated = current_timestamp;
end
</pre>


<h2>Additional Information</h2>

<p>new - the new version of the record with proposed values.</p>
<p>old - the old version of the row with current values.</p>


<div class="footer"><hr />
<p>Page author: USP &bull; Page editor: USP &bull; Last update: 2013-06-19 &bull; 1</p>
<p>&copy; 2013 Synesis Ulrich Sprick. All rights reserved. See <a href="../../../copyright.htm">Copyright details</a>.</p>
<p><a href="#top">Back to Top</a> | <a href="../../../index.htm" target="_top">Home</a> | <a id="reload" onclick="reloadFrameset()" href ="../../../index.htm" target="_top" >Reload Frameset</a></p>
</div></body></html>
